package com.tp.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
/**
 * 路由拦截器
 * 
 * @author TP
 *
 */
public class RouteInterceptor implements Interceptor {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jfinal.aop.Interceptor#intercept(com.jfinal.aop.Invocation)
	 */
	public void intercept(Invocation inv) {
		Controller c = inv.getController();
		c.renderNull();
		String ck = inv.getControllerKey();
		int lastIndex = ck.lastIndexOf("/");
		if (lastIndex > 0) {
			String g = ck.substring(1, lastIndex);
			c.setAttr("GROUP_NAME", g);
			String m = ck.substring(lastIndex + 1);
			c.setAttr("MODULE_NAME", m);
		}
		String a = inv.getMethodName();
		c.setAttr("ACTION_NAME", a);
		Boolean isGet = c.getRequest().getMethod().equals("GET");
		c.setAttr("IS_GET", isGet);
		Boolean isPost = !isGet;
		c.setAttr("IS_POST", isPost);
		String requestType = c.getRequest().getHeader("X-Requested-With");
		Boolean isAjax = (requestType != null && requestType.equals("XMLHttpRequest"));
		c.setAttr("IS_AJAX", isAjax);
		c.setAttr("PART", c.getPara());
		inv.invoke();
	}
}